<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核记录ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入审核记录ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓氏" prop="surname">
        <el-input
          v-model="queryParams.surname"
          placeholder="请输入姓氏"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态">
          <el-option
            v-for="dict in audit_has_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['qth:driverReview:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qth:driverReview:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qth:driverReview:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['qth:driverReview:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="driverReviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-space direction="vertical" size="8" align="start">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['qth:driverReview:edit']">详情</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['qth:driverReview:remove']">删除</el-button>
            </el-space>
          </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template #default="scope">
          <dict-tag :options="audit_has_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核记录ID" align="center" prop="id" width="100px"/>
      <el-table-column label="司机ID" align="center" prop="driverId" width="100px"/>
      <el-table-column label="头像图片" align="center" prop="profileImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.profileImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="姓氏" align="center" prop="surname" />
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="has_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="phone" width="120px"/>
      <el-table-column label="紧急联系人" align="center" prop="emergencyContact" width="100px"/>
      <el-table-column label="紧急联系人电话" align="center" prop="emergencyContactPhone" width="120px"/>
      <el-table-column label="身份证" align="center" prop="idCard" width="150px"/>
      <el-table-column label="身份证照片正面" align="center" prop="idCardFrontImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.idCardFrontImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证照片反面" align="center" prop="idCardBackImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.idCardBackImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="驾驶证照片正面" align="center" prop="driverCardFrontImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.driverCardFrontImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="驾驶证照片反面" align="center" prop="driverCardBackImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.driverCardBackImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="驾驶证领取时间" align="center" prop="driverCardReceiveTime" width="150px">
        <template #default="scope">
          <span>{{ parseTime(scope.row.driverCardReceiveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="驾驶证到期时间" align="center" prop="driverCardExpriesTime" width="150px">
        <template #default="scope">
          <span>{{ parseTime(scope.row.driverCardExpriesTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="驾龄" align="center" prop="driverAge" />
      <el-table-column label="准驾车型" align="center" prop="driverCarType">
        <template #default="scope">
          <dict-tag :options="driver_has_car_type" :value="scope.row.driverCarType"/>
        </template>
      </el-table-column>
      <el-table-column label="银行名称" align="center" prop="bankName" />
      <el-table-column label="银行卡号" align="center" prop="bankCardId" width="170px"/>
      <el-table-column label="驳回原因" align="center" prop="auditReason" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改司机认证列表对话框 -->
    <el-dialog :title="title" v-model="open" width="1500px" append-to-body>
      <el-form ref="driverReviewRef" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="头像图片" prop="profileImg">
              <image-preview :src="form.profileImg" style="width: 100px; height: 100px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in has_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓氏" prop="surname">
              <el-input v-model="form.surname" placeholder="请输入姓氏" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名字" prop="name">
              <el-input v-model="form.name" placeholder="请输入名字" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="紧急联系人" prop="emergencyContact">
              <el-input v-model="form.emergencyContact" placeholder="请输入紧急联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急联系人电话" prop="emergencyContactPhone">
              <el-input v-model="form.emergencyContactPhone" placeholder="请输入紧急联系人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="身份证照片正面" prop="idCardFrontImg">
          <image-preview :src="form.idCardFrontImg" style="width: 150px; height: 150px;"/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证照片反面" prop="idCardBackImg">
              <image-preview :src="form.idCardBackImg" style="width: 150px; height: 150px;"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="驾驶证照片正面" prop="driverCardFrontImg">
              <image-preview :src="form.driverCardFrontImg" style="width: 150px; height: 150px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驾驶证照片反面" prop="driverCardBackImg">
              <image-preview :src="form.driverCardBackImg" style="width: 150px; height: 150px;"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="驾驶证领取时间" prop="driverCardReceiveTime">
              <el-date-picker clearable
                v-model="form.driverCardReceiveTime"
                type="date"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择驾驶证领取时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驾驶证到期时间" prop="driverCardExpriesTime">
              <el-date-picker clearable
                v-model="form.driverCardExpriesTime"
                type="date"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择驾驶证到期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="驾龄" prop="driverAge">
              <el-input v-model="form.driverAge" placeholder="请输入驾龄" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="准驾车型" prop="driverCarType">
              <el-select v-model="form.driverCarType" placeholder="请选择准驾车型">
                <el-option
                  v-for="dict in driver_has_car_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="银行名称" prop="bankName">
              <el-input v-model="form.bankName" placeholder="请输入银行名称" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行卡号" prop="bankCardId">
              <el-input v-model="form.bankCardId" placeholder="请输入银行卡号" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="审核状态" prop="auditStatus">
              <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
                <el-option
                  v-for="dict in audit_has_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value * 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.auditStatus === 2">
            <el-form-item label="驳回原因" prop="auditReason">
              <el-input v-model="form.auditReason" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DriverReview">
import { listDriverReview, getDriverReview, delDriverReview, addDriverReview, updateDriverReview } from "@/api/qth/driverReview";

const { proxy } = getCurrentInstance();
const { driver_has_car_type, audit_has_status, has_sex } = proxy.useDict('driver_has_car_type', 'audit_has_status', 'has_sex');

const driverReviewList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    surname: null,
    name: null,
    idCard: null,
    auditStatus: null,
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询司机认证列表列表 */
function getList() {
  loading.value = true;
  listDriverReview(queryParams.value).then(response => {
    driverReviewList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    profileImg: null,
    surname: null,
    name: null,
    sex: null,
    phone: null,
    emergencyContact: null,
    emergencyContactPhone: null,
    idCard: null,
    idCardFrontImg: null,
    idCardBackImg: null,
    driverCardFrontImg: null,
    driverCardBackImg: null,
    driverCardReceiveTime: null,
    driverCardExpriesTime: null,
    driverAge: null,
    driverCarType: null,
    auditStatus: null,
    auditReason: null,
    bankName: null,
    bankCardId: null,
  };
  proxy.resetForm("driverReviewRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加司机认证列表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDriverReview(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改司机认证列表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["driverReviewRef"].validate(valid => {
    if (valid) {
      //保存审核记录
      if (form.value.id != null) {
        updateDriverReview(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDriverReview(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除司机认证列表编号为"' + _ids + '"的数据项？').then(function() {
    return delDriverReview(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('qth/driverReview/export', {
    ...queryParams.value
  }, `driverReview_${new Date().getTime()}.xlsx`)
}
getList();
</script>
